package com.light.wanting.emoji.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.light.applet.common.result.Result;
import com.light.applet.common.result.message.ResultMessage;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: 魏彪
 */
@Component
public class EmojiAuthenticationEntryPoint implements AuthenticationEntryPoint {

    private static final String CONTENT_TYPE = "application/json;charset=UTF-8";

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        response.setContentType(CONTENT_TYPE);
        response.getWriter()
                .write(objectMapper.writeValueAsString(Result.failed(ResultMessage.UN_AUTHENTICATION_FAILED)));
    }
}
